package com.cskaoyan.controller.stage;

import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.CollectorService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Map;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

/**
 * @program: 微信小程序
 * @description:
 * @author: Wangtianhao
 * @create: 2022-05-08 22:27
 **/

@RestController
@RequestMapping("wx/collect")
public class WxCollectorHandler {

    @Autowired
    CollectorService collectorService;

    /*
     * description 获取用户信息
     * @author Wangtianhao
     * @date 2022-05-10 14:21
     */
    public Object getUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            //在已经认证成功的情况下，可以获得用户信息
            // 获得的用户信息的来源 → 来源realm的doGetAuthenticationInfo方法的返回值的第一个参数
            Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
            return primaryPrincipal;
        }
        return ExceptionVO.error(700, "账号未登录");
    }
    /*
     * description 收藏夹列表
     * @author Wangtianhao
     * @date 2022-05-09 10:50
     */

    @GetMapping("list")
    public CollectorRespVO collectorList(Integer userId, BaseParamBO param) {
        CollectorDataVO collectorDataVO = collectorService.queryCollector(userId, param);
        return CollectorRespVO.ok(collectorDataVO);
    }

    /*
     * description 收藏夹添加删除
     * @author Wangtianhao
     * @date 2022-05-09 10:50
     */

    @PostMapping("addordelete")
    public ExceptionVO addorDelete(@RequestBody Map body) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer valueId = Integer.parseInt(body.get("valueId").toString());
            Integer type = Integer.parseInt(body.get("type").toString());
            collectorService.addOrDelete(valueId, type);
            return ExceptionVO.fine();
        }
        return ExceptionVO.error(501, "请先登录");
    }
}
